#include <iostream>

using namespace std;
typedef long long LL;

int exgcd(int a, int b, int &x, int &y)
{
    if (!b)
    {
        x = 1, y = 0;
        return a;
    }
    int t = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return t;
}

int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        int a, b, m;
        cin >> a >> b >> m;
        int x = 0, y = 0;
        int gcd = exgcd(a, m, x, y);
        // 如果b是最大公约数的倍数，那么一定有结果
        // 记得%m确保数据不超过int
        if (b % gcd == 0)
            cout << (LL)x * (b / gcd) % m << endl;
        else
            cout << "impossible\n";
    }
    return 0;
}
